import apis from '@/apis'

const initProductPageInfo = {
  pageIndex: 1,
  pageSize: 20
}

export default {
  namespaced: true,
	state: {
		categoryList: [],
		brandList: [],
		seriesList: [],
		modelProductList: [],
		activeCategory: '',
		activeBrand: '',
		activeSeries: '',
		categoryLoadStatus: false,
		brandLoadStatus: false,
		seriesLoadStatus: false,
		productLoadStatus: false,
		productFirstStatus: false,
		productPageInfo: {
			...initProductPageInfo
		},
		finished: false
	},
	mutations: {
		setData (state, { key, value }) {
			state[key] = value
		}
	},
	actions: {
		resetLoadingStatus ({ commit }) {
			commit('setData', { key: 'categoryLoadStatus', value: false })
			commit('setData', { key: 'brandLoadStatus', value: false })
			commit('setData', { key: 'seriesLoadStatus', value: false })
			commit('setData', { key: 'productLoadStatus', value: false })
			commit('setData', { key: 'productFirstStatus', value: true })
		},
		/*
		 * 类目列表
		*/
		async getCategoryList ({ dispatch, commit, state }, { targetClassId = '', targetBrandId = '' }) {
			commit('setData', { key: 'categoryLoadStatus', value: true })
			const [res] = await apis.getCategoryList()
			commit('setData', { key: 'categoryLoadStatus', value: false })
			if (res._errCode === '0') {
				const classList = res._data.classList || []
				commit('setData', { key: 'categoryList', value: classList })
				if (classList.length) {
					if (!state.activeCategory) {
						let selectClassId = classList[0].classId
						if (targetClassId) {
							if (classList.some(e => e.classId === targetClassId)) {
								selectClassId = targetClassId
							}
						}
						commit('setData', { key: 'activeCategory', value: selectClassId })
					}
					dispatch('getBrandList', { targetBrandId })
				}
			}
		},
		/*
		 * 品牌列表
		*/
		async getBrandList ({ dispatch, commit, state }, { targetBrandId }) {
			commit('setData', { key: 'brandLoadStatus', value: true })
			const [res] = await apis.getBrandList({
				classId: state.activeCategory,
				pageSize: 100
			})
			commit('setData', { key: 'brandLoadStatus', value: false })
			if (res._errCode === '0') {
				const brandList = res._data.brandList || []
				commit('setData', { key: 'brandList', value: brandList })
				if (brandList.length) {
					if (!state.activeBrand) {
						let selectBrandId = brandList[0].brandId
						if (targetBrandId) {
							if (brandList.some(e => e.brandId === targetBrandId)) {
								selectBrandId = targetBrandId
							}
						}
						commit('setData', { key: 'activeBrand', value: selectBrandId })
					}
					dispatch('getSeriesList', { targetBrandId })
				}
			}
		},
		/*
		 * 系列列表
		*/
		async getSeriesList ({ dispatch, commit, state }, {}) {
			commit('setData', { key: 'seriesLoadStatus', value: true })
			const [res] = await apis.getSeriesList({
				classId: state.activeCategory,
				brandId: state.activeBrand
			})
			commit('setData', { key: 'seriesLoadStatus', value: false })
			if (res._errCode === '0') {
				const seriesList = res._data.seriesList || []
				commit('setData', { key: 'seriesList', value: seriesList })
				commit('setData', { key: 'activeSeries', value: seriesList[0].seriesId || '' })
				dispatch('getProductList', {})
			}
		},
		/*
		 * 机型列表
		*/
		async getProductList ({ commit, state }, { more = false }) {
			const { modelProductList, productPageInfo, productFirstStatus } = state
			commit('setData', { key: 'productLoadStatus', value: true })
			const pageIndex = more ? Number(productPageInfo.pageIndex) + 1 : 1
			const pageSize = productPageInfo.pageSize
			const [res] = await apis.getProductList({
				pid: '1539',
				classId: state.activeCategory,
				brandId: state.activeBrand,
				seriesId: state.activeSeries,
				pageIndex,
				pageSize
			})
			commit('setData', { key: 'productLoadStatus', value: false })
			if (res._errCode === '0') {
				let list = res._data.productList || []
				const pageInfo = res._data.pageInfo || {}
				list = more ? modelProductList.concat(list) : list
				commit('setData', { key: 'modelProductList', value: list })
				commit('setData', { key: 'productPageInfo', value: { ...productPageInfo, ...pageInfo }})
			}
			if (!productFirstStatus) {
				commit('setData', { key: 'productFirstStatus', value: true })
			}
		},
		/*
		 * 切换类目
		*/
		changeCategory ({ dispatch, commit }, { classId }) {
			commit('setData', { key: 'activeCategory', value: classId })
			commit('setData', { key: 'activeBrand', value: '' })
			commit('setData', { key: 'brandList', value: [] })
			commit('setData', { key: 'seriesList', value: [] })
			commit('setData', { key: 'modelProductList', value: [] })
			dispatch('getBrandList', {})
		},
		/*
		 * 切换品牌
		*/
		changeBrand ({ dispatch, commit }, { brandId }) {
			commit('setData', { key: 'activeBrand', value: brandId })
			commit('setData', { key: 'activeSeries', value: '' })
			commit('setData', { key: 'seriesList', value: [] })
			commit('setData', { key: 'modelProductList', value: [] })
			dispatch('getSeriesList', {})
		},
		/*
		 * 切换系列
		*/
		changeSeries ({ dispatch, commit }, { seriesId }) {
			commit('setData', { key: 'activeSeries', value: seriesId })
			commit('setData', { key: 'modelProductList', value: [] })
			dispatch('getProductList', {})
		}
	}
}